package pimp.organizermanager;

import java.io.File;
import javax.swing.tree.TreeModel;
import pimp.GFX;
import pimp.financemanager.FinanceManager;
import java.util.*;
import netcom.DBManager;

public class OrganizerManager {

    final private FinanceManager financeManager = FinanceManager.getInstance();
    final private CalendarGroup calendarGroup = CalendarGroup.getInstance();
    final private GFX gfx = GFX.getInstance(null);
    final private DBManager dbmanager = DBManager.getInstance();

    private OrganizerManager(){
        System.out.println("OrganizerManager initialization completed!");
    }

    private static class Singleton {
        private final static OrganizerManager instance = new OrganizerManager();
    }

    public static OrganizerManager getInstance() {
        return Singleton.instance;
    }

    public static TreeModel LoadCategoriesFromFile(File file) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public void addGlobalEvent(GlobalEvent globalEvent) {
        calendarGroup.addGlobalEvent(globalEvent);

    }

    public boolean createEvent(String id, Date beginDate, Date endDate, String description,
            boolean cyclic) {
        Event event = new Event(id, beginDate, endDate, description);   //koniecznie poprawić, gdyż nie obsługiwane jest wsadzanie ventów do drzewa, którym nie nada się ID!
        event.setCyclic(cyclic);
        beginDate.printDate();
        endDate.printDate();
        if(calendarGroup.addEvent(event)) {
            System.out.println("Event successfully added\n");
            return true;
        }
        System.out.println("Failure. Event not added\n");
        return false;
    }

    public Date getCurrentDate() {
        return calendarGroup.getCurrentDate();
    }

    public Rule createRule(int xYears, int xMonths, int xDays, Vector<Date> exceptions) {
        if(xYears < 0 || xMonths < 0 || xDays < 0) {
            return null;
        }
        return new Rule(xYears, xMonths, xDays, exceptions);
    }

    public boolean createCycle(String id, Vector<Date> beginDates, Date endDate, String description,
            Rule rule) {
        Event event = new Event(id, new Date(0, 0, 0), new Date(0, 0, 0),
                description);
        event.setCyclic(true);

        if(rule == null) {
            return false;
        }

        Cycle cycle = new Cycle(event, beginDates, endDate, rule);
        if(calendarGroup.addCycle(cycle)) {
            return true;
        }
        else {
            return false;
        }
    }

    public boolean getGlobalEventByDate(Date beginDate, Date endDate) {
        System.out.println("1.");
        beginDate.printDate();
        endDate.printDate();
        Map<ID, GlobalEvent> map = calendarGroup.getGlobalEventByDate
                (beginDate, endDate);
        System.out.println("Elements found:\n");
        Iterator iterator = map.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            GlobalEvent globalEvent = (GlobalEvent) entry.getValue();
            System.out.println(globalEvent.getDescription() + "\n");
        }
        return true;
    }

    public void saveDataBaseToFile() {
        dbmanager.saveDataBaseToFile();
    }

    public void saveDataBaseToServer() {
        dbmanager.saveDataBaseToServer();
    }

    TreeModel getCategoriesTreeModel() {
        return GFX.getInstance(null).getCategoriesTreeModel();
    }

  }
